<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="流程名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入流程名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="流程分类" prop="category">
        <el-select
          v-model="queryParams.category"
          clearable
          placeholder="请选择流程分类"
        >
          <el-option
            v-for="item in categoryOptions"
            :key="item.categoryId"
            :label="item.categoryName"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select
          v-model="queryParams.state"
          clearable
          placeholder="请选择状态"
        >
          <el-option :key="1" label="激活" value="active" />
          <el-option :key="2" label="挂起" value="suspended" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-table
        v-loading="loading"
        fit
        :data="deployList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="流程名称" :show-overflow-tooltip="true">
          <template #default="scope">
            <el-button
              type="primary"
              text
              @click="handleProcessView(scope.row)"
            >
              <span>{{ scope.row.processName }}</span>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="流程分类"
          prop="categoryName"
          :formatter="categoryFormat"
        />
        <el-table-column label="流程版本">
          <template #default="scope">
            <el-tag>v{{ scope.row.version }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="scope">
            <el-tag type="success" v-if="!scope.row.suspended">激活</el-tag>
            <el-tag type="warning" v-if="scope.row.suspended">挂起</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="部署时间" prop="deploymentTime" width="180" />
        <el-table-column
          align="center"
          label="操作"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <!-- v-hasPermi="['workflow:deploy:list']" -->
            <el-button
              type="primary"
              text
              icon="el-icon-price-tag"
              @click="handlePublish(scope.row)"
              >版本管理</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>

    <!-- 流程图 -->
    <el-dialog
      :title="processView.title"
      v-model="processView.open"
      width="70%"
      append-to-body
    >
      <my-process-viewer
        key="designer"
        :value="processView.xmlData"
        v-bind="bpmnControlForm"
        :prefix="bpmnControlForm.prefix"
        :activityData="activityList"
        :processInstanceData="processInstance"
        :taskData="tasks"
      />
    </el-dialog>

    <!-- 版本管理 -->
    <el-dialog
      title="版本管理"
      v-model="publish.open"
      width="700"
      append-to-body
    >
      <el-table v-loading="publish.loading" :data="publish.dataList">
        <el-table-column label="流程名称" :show-overflow-tooltip="true">
          <template #default="scope">
            <el-button
              type="primary"
              text
              @click="handleProcessView(scope.row)"
            >
              <span>{{ scope.row.processName }}</span>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="流程版本" width="100">
          <template #default="scope">
            <el-tag>v{{ scope.row.version }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template #default="scope">
            <el-tag type="success" v-if="!scope.row.suspended">激活</el-tag>
            <el-tag type="warning" v-if="scope.row.suspended">挂起</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="150"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              type="primary"
              text
              size="small"
              class="inline-block"
              icon="el-icon-video-pause"
              v-if="!scope.row.suspended"
              @click="handleChangeState(scope.row, 'suspended')"
              >挂起</el-button
            >
            <el-button
              type="primary"
              text
              size="small"
              class="inline-block"
              icon="el-icon-video-play"
              v-if="scope.row.suspended"
              @click="handleChangeState(scope.row, 'active')"
              >激活</el-button
            >
            <el-button
              type="primary"
              text
              size="small"
              class="inline-block"
              icon="el-icon-delete"
              @click="handleDelete(scope.row, 'model')"
              >删除</el-button
            >
            <!-- v-hasPermi="['workflow:deploy:remove']" -->
            <!-- v-hasPermi="['workflow:deploy:status']" -->
            <!-- v-hasPermi="['workflow:deploy:status']" -->
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="publishTotal > 0"
        :total="publishTotal"
        v-model:page="publishQueryParams.pageNum"
        v-model:limit="publishQueryParams.pageSize"
        @pagination="getPublishList"
      />
    </el-dialog>
  </div>
</template>

<script name="Deploy" setup>
import { useRoute, useRouter } from 'vue-router'
import { listAllCategory } from '@/api/workflow/category'
import {
  listDeploy,
  listPublish,
  getBpmnXml,
  changeState,
  delDeploy
} from '@/api/workflow/deploy'
import myProcessViewer from '@/components/bpmnProcessDesigner/package/designer/ProcessViewer.vue'
const router = useRouter()
const { proxy } = getCurrentInstance()
const data = reactive({
  // 遮罩层
  loading: true,
  // 选中数组
  ids: [],
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  // 总条数
  total: 0,
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    processKey: null,
    processName: null,
    category: null,
    state: null
  },
  deployList: [],
  categoryOptions: [],
  processView: {
    title: '',
    open: false,
    index: undefined,
    xmlData: ''
  },
  publish: {
    open: false,
    loading: false,
    dataList: []
  },
  publishTotal: 0,
  publishQueryParams: {
    pageNum: 1,
    pageSize: 10,
    processKey: ''
  }
})
const {
  // 遮罩层
  loading,
  // 选中数组
  ids,
  // 非单个禁用
  single,
  // 非多个禁用
  multiple,
  // 显示搜索条件
  showSearch,
  // 总条数
  total,
  // 查询参数
  queryParams,
  deployList,
  categoryOptions,
  processView,
  publish,
  publishTotal,
  publishQueryParams
} = toRefs(data)
/** 查询流程分类列表 */
function getCategoryList() {
  listAllCategory().then(response => (categoryOptions.value = response.data))
}
/** 查询流程部署列表 */
function getList() {
  loading.value = true
  listDeploy(queryParams.value).then(response => {
    deployList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryForm')
  handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.deploymentId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
// ========== 高亮流程图 start ==========
const bpmnControlForm = ref({
  prefix: 'flowable'
})
const activityList = ref([])
const processInstance = ref({})
const tasks = ref([])
/** 查看流程图 */
const handleProcessView = row => {
  let definitionId = row.definitionId
  processView.value.title = '流程图'
  processView.value.index = definitionId
  // 发送请求，获取xml
  getBpmnXml(definitionId).then(response => {
    processView.value.xmlData = response.data
  })
  processView.value.open = true
}
// ========== 高亮流程图 end ==========
//查询版本管理列表数据
function getPublishList() {
  publish.loading = true
  listPublish(publishQueryParams.value).then(response => {
    publish.value.dataList = response.rows
    publishTotal.value = response.total
    publish.value.loading = false
  })
}
//点击版本管理按钮
function handlePublish(row) {
  publishQueryParams.value.processKey = row.processKey
  publish.value.open = true
  getPublishList()
}
/** 挂起/激活流程 */
function handleChangeState(row, state) {
  const params = {
    definitionId: row.definitionId,
    state: state
  }
  changeState(params).then(res => {
    proxy.$modal.msgSuccess('操作成功')
    getPublishList()
  })
}
function handleDelete(row, type) {
  const deploymentIds = row.deploymentId || ids.value
  proxy.$modal
    .confirm('是否确认删除选中的数据项？')
    .then(() => {
      loading.value = true
      return delDeploy(deploymentIds)
    })
    .then(() => {
      loading.value = false

      proxy.$modal.msgSuccess('删除成功')
      if (type == 'model') {
        getPublishList()
      } else {
        getList()
      }
    })
    .finally(() => {
      loading.value = false
    })
}
function categoryFormat(row, column) {
  return (
    categoryOptions.value.find(k => k.code === row.category)?.categoryName ?? ''
  )
}

getCategoryList()
getList()
</script>
